package cn.edu.zafu.bigdata.zengyadian;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RhymingWords {

    public static void findRhymingWords() {
        // 构建words.txt文件的路径
        File wordsFile = new File("files/words.txt");
        Map<String, List<String>> rhymingWordsMap = new HashMap<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(wordsFile))) {
            String line;
            while ((line = reader.readLine())!= null) {
                String word = line.trim();
                // 简单取最后三个字符作为判断押韵的依据
                String lastThreeChars = word.substring(Math.max(0, word.length() - 3));
                if (!rhymingWordsMap.containsKey(lastThreeChars)) {
                    rhymingWordsMap.put(lastThreeChars, new ArrayList<>());
                }
                rhymingWordsMap.get(lastThreeChars).add(word);
            }

            // 输出押韵单词组
            for (Map.Entry<String, List<String>> entry : rhymingWordsMap.entrySet()) {
                if (entry.getValue().size() > 1) {
                    System.out.println("Rhyming words with ending " + entry.getKey() + ": " + entry.getValue());
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        findRhymingWords();
    }
}